const path = require('path');
const isDev = process.env.NODE_ENV === 'development';
const isV2 = process.env.VUE_APP_VERSION === 'v2'

const resolveApp = relativePath => path.join(__dirname, relativePath);

const outputDir =  isV2 ? 'dist/v2' : 'dist/v1'
module.exports = {
  publicPath: './',
  outputDir,
  assetsDir: 'static',
  productionSourceMap: false,
  configureWebpack: {
    output: {
      filename: `js/[name].[${isDev ? 'hash' : 'contenthash:8'}].js`,
      chunkFilename: 'js/[name].[chunkhash:8].js'
    },
    resolve: {
      alias: {
        '@v2': resolveApp('src/pages/v2')
      }
    }
  },
  devServer: {
    open: true,
    proxy: {
      '/api': {
        target:'http://127.0.0.1:4040/api/',
        // target: 'http://163.177.38.6:3240/api/',
        changeOrigin: true,
        pathRewrite: {
          '/api': ''
        }
      }
    }
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'sass',
      patterns: [path.resolve(__dirname, './src/assets/scss/*.scss')]
    }
  },
  chainWebpack(config) {
    config.when(!isDev, (config) => {
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          echarts: {
            name: 'chunk-echarts',
            test: /[\\/]node_modules[\\/]echarts[\\/]/,
            minSize: 0,
            minChunks: 1,
            reuseExistingChunk: true,
            chunks: 'all'
          },
          element: {
            name: 'chunk-element',
            priority: 20,
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/
          },
          commons: {
            name: 'chunk-commons',
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          }
        }
      });
      // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
      config.optimization.runtimeChunk('single');
    });
  }
};